﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using DARE.Save.DataStructures;

namespace DARE
{
    public class CEmitterData : ISaveData
    {
        public int EmitPerEmission { get; set; }

        public int EmissionInterval { get; set; }

        public int MaxParticlesSimultaneous { get; set; }

        public CParticleData ReferenceParticle { get; set; }

        public bool IsStarted { get; set; }

        public string Name { get; set; }

        public Vector3 Velocities { get; set; }

        public virtual void FillDataStructure(object e)
        {
            AEmitter emitter = e as AEmitter;
            if (emitter == null)
                return;
            Name = emitter.Name;
            EmissionInterval = emitter.EmissionInterval;
            EmitPerEmission = emitter.EmitPerEmission;
            MaxParticlesSimultaneous = emitter.MaxParticlesSimultaneous;
            AttrTypeDependentSerializable att = emitter.ReferenceParticle.GetType().GetCustomAttributes(typeof(AttrTypeDependentSerializable), false).First() as AttrTypeDependentSerializable;
            Type t = att.TypeOfDataStructure;
            if (t == null)
                t = typeof(CParticleData);
            ReferenceParticle = (CParticleData)t.GetConstructor(new Type[0]).Invoke(new object[0]);
            ReferenceParticle.FillDataStructure(emitter.ReferenceParticle);
            IsStarted = emitter.IsStarted;
            Velocities = emitter.Velocities;
        }

        public virtual void FillFromDataStructure(out object e, params object[] paramsData)
        {
            e = CreateEmitter();
            AEmitter emitter = e as AEmitter;
            if (emitter == null)
                return;
            emitter.Name = Name;
            emitter.EmissionInterval = EmissionInterval;
            emitter.EmitPerEmission = EmitPerEmission;
            emitter.MaxParticlesSimultaneous = MaxParticlesSimultaneous;
            object part;
            ReferenceParticle.FillFromDataStructure(out part);
            emitter.ReferenceParticle = part as IParticle;
            emitter.IsStarted = IsStarted;
            emitter.Velocities = Velocities;
        }

        public virtual AEmitter CreateEmitter()
        {
            return null;
        }
    }
}
